package com.team8.search.listener;

import com.team8.search.pojo.FlightInfoES;
import com.team8.search.repository.FlightInfoRepository;
import com.team8.system.pojo.FlightInfo;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class FlightInfoListener {

    @Autowired
    private FlightInfoRepository flightInfoRepository;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "as.ex.flightinfo.insert",
                    durable = "true"
            ),     // 声明队列
            exchange = @Exchange(
                    value = "as.es.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"es.flightinfo.insert"}
    )
    )
    public void listenFlightInfoInsert(FlightInfo flightInfo) throws Exception {

        FlightInfoES flightInfoES = new FlightInfoES(
                flightInfo.getFfid(),
                flightInfo.getStartPort(),
                flightInfo.getEndPort(),
                flightInfo.getTicketNum(),
                flightInfo.getStartTime().getTime(),
                flightInfo.getEndTime().getTime(),
                flightInfo.getStartSeg(),
                flightInfo.getEndSeg());

        flightInfoES.setId(String.valueOf(flightInfo.getFlightId()));

        // 保存在es中
        FlightInfoES save = flightInfoRepository.save(flightInfoES);

        System.out.println(save);
        redisTemplate.opsForValue().set(save.getFfid() + ":" + save.getStartSeg() + ":"+save.getEndSeg() + ":id", save.getId());

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "as.ex.flightinfo.update",
                    durable = "true"
            ),     // 声明队列
            exchange = @Exchange(
                    value = "as.es.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"es.flightinfo.update"}
    )
    )
    public void listenFlightInfoUpdate(String msg) throws Exception {
        String[] split = msg.split(":");
        String mqFfid = split[0];
        String mqStartSeg = split[1];
        String mqEndSeg = split[2];
        String mqTicketNum = split[3];

        String key = mqFfid + ":" + mqStartSeg + ":" + mqEndSeg;
        // 分段的格式是 ffid startSeg endSeg ticketNum
        String ticket = redisTemplate.opsForValue().get(key);

        // 如果redis中的票和接收到的消息的票一直，睡1秒钟
        if(ticket.equals(mqTicketNum)){
            Thread.sleep(1000);

            // 睡一秒钟之后如果还一致，更新数据库
            ticket = redisTemplate.opsForValue().get(key);
            if(ticket.equals(mqTicketNum)){

                String id = redisTemplate.opsForValue().get(key + ":id");
                Optional<FlightInfoES> flightInfoESs = flightInfoRepository.findById(id);
                FlightInfoES flightInfoES = flightInfoESs.get();
                System.out.println("flightInfoES  from ex :     "  + flightInfoES);
                flightInfoES.setTickets(Integer.valueOf(ticket));
                flightInfoRepository.save(flightInfoES);

            }
        }
    }
}
